package com.weixing.mall.util;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.UUID;

/**
 * @ClassName CommonUtil
 * @Description
 * @Author Small
 * @Date 2020/3/10 15:38
 * @Version 1.0
 **/
public class CommUtil {

    /**
     * 方法描述: 隐藏姓名
     * @Author Small
     * @param realName
     * @Date 2020/3/10 15:41
     * @return java.lang.String
     */
    public static String coverRealName(String realName) {
        return StrUtil.replace(realName, 1, realName.length(), '*');
    }


    /**
     * 隐藏手机号
     * @param mobile 手机号
     * @return  隐藏后的手机号
     */
    public static String coverMobile(String mobile) {
        return StrUtil.replace(mobile, 4, 7, '*');
    }

    /**
     * 隐藏身份证号
     *
     * @param idCard 身份证号
     * @return  隐藏后的身份证号
     */
    public static String coverIdcard(String idCard) {
        return StrUtil.replace(idCard, 4, 14, '*');
    }


    public static String[] chars = new String[]{"a", "b", "c", "d", "e", "f",
            "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
            "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z"};


    public static String uuid(int uuidLength, Object splicing) {
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < uuidLength; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 0x3E]);
        }
        return shortBuffer.toString().toLowerCase() + splicing;

    }

    /**
     * 验证路径是否有效
     * @param urlStr
     */
    public static boolean checkUrl(String urlStr){
        try {
            URL url = new URL(urlStr);
            HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();
            urlcon.setRequestMethod("POST");
            urlcon.setRequestProperty("Content-type",
                    "application/x-www-form-urlencoded");
            if (urlcon.getResponseCode() == HttpURLConnection.HTTP_OK) {
                System.out.println(urlStr+":ok");
                return true;
            } else {
                System.out.println(urlStr+":faild");
               return false;
            }
        }catch (Exception e){
             e.printStackTrace();
        }finally {
            return false;
        }
    }

    public static void main(String[] args) {
//        String tt = "21";
        /* System.out.println(generateShortUuid(8-tt.length(),21));*/


//        System.out.println(coverMobile("18736000723"));
        System.out.println(coverIdcard("411328199003239901"));

        Date now = new Date();
        DateTime parse = DateUtil.parse("20210829", DatePattern.PURE_DATE_PATTERN);
        int compare = DateUtil.compare(now, parse);
        System.out.println(compare);
    }
}
